/**
 Redistribution and use in source and binary forms, with or
 without modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer
 in the documentation and/or other materials provided with the distribution.
 * Neither the names of the authers, nor the names of its contributors, may be used to endorse or 
 promote products derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE REGENTS/COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 REGENTS/COPYRIGHT OWNERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 @Author Fredrique Samuels <fredriquesamuels@gmail.com>
 */

#ifndef TEST_NODE_H_
#define TEST_NODE_H_

#include <time.h>

#include "testcase.h"
#include "../src/node.h"

class test_node : public testcase {
public:
	test_node() :
		testcase() {
	}
	;
	virtual ~test_node() {
	}
	;

	class NumberNode : public tri::node::Node {
public:
		NumberNode(int num) :
			tri::node::Node() {
			this->num = num;
		}
		virtual ~NumberNode() {
		}

		inline int getSum() {
			int total = num;
			if (getNextNode() != NULL) {
				total+= ( (NumberNode*)getNextNode())->getSum();
			}

			if (getChildNode() != NULL) {
				total+= ( (NumberNode*)getChildNode())->getSum();
			}
			return total;
		}

		inline void addNumberNode(NumberNode* n) {
			this->addNode(n);
		}

		inline void removeNumberNode(NumberNode* n) {
			this->removeNode(n);
		}

		inline void assertDefault() {
			assert(getPrevNode() == NULL);
			assert(getNextNode() == NULL);
			assert(getChildNode() == NULL);
			assert(getRootNode() == NULL);
			//			assert(getLeafNode() == NULL);
		}

		inline void assertLeafNode(Node* n) {
			assert(getLeafNode() == n);
		}

		inline void assertChildNode(Node* n) {
			assert(getChildNode() == n);
		}

		inline void assertRootNode(Node* n) {
			assert(getRootNode() == n);
		}

		inline bool isChild(Node* n) {
			return inChildNode(n);
		}

private:
		int num;
	};

	inline virtual void run_tests() {
		cout<<"\n*********************************"<<endl;
		cout<<"*"<<endl;
		cout<<"* Running Node Tests"<<endl;
		cout<<"*"<<endl;
		cout<<"*********************************"<<endl;

		testNode();
		testNodeSearch();
	}

	inline void testNode() {
		NumberNode node1(1);
		NumberNode node2(2);
		NumberNode node3(4);
		NumberNode node4(8);
		NumberNode node5(16);

		node1.assertDefault();
		assert(node1.getSum()==1);

		assert(!node1.isChild(&node2));
		node1.addNumberNode(&node2);
		assert(node1.isChild(&node2));
		node2.assertLeafNode(&node2);
		node2.assertRootNode(&node1);
		node1.assertChildNode(&node2);
		assert(node2.getSum()==2);
		assert(node1.getSum()==3);

		assert(!node1.isChild(&node3));
		node1.addNumberNode(&node3);
		assert(node1.isChild(&node3));

		node2.assertLeafNode(&node3);
		node2.assertRootNode(&node1);
		node3.assertRootNode(&node1);
		node3.assertLeafNode(&node3);
		node1.assertChildNode(&node2);
		node1.assertLeafNode(NULL);
		node1.assertRootNode(NULL);
		assert(node3.getSum()==4);
		assert(node2.getSum()==6);
		assert(node1.getSum()==7);

		node1.removeNumberNode(&node2);
		assert(!node1.isChild(&node2));
		node1.assertChildNode(&node3);
		assert(node2.getSum()==2);
		assert(node1.getSum()==5);

	}
	
	void testNodeSearch(){
		tri::node::Node node1,node2,node3,node4,node5,node6;
		
		node1.addNode(&node2);
		node2.addNode(&node3);
		node3.addNode(&node4);
		node3.addNode(&node5);
//		node5.addNode(&node6);
		
		assert(node1.inChildNodeRecursive(&node2));
		assert(node1.inChildNodeRecursive(&node3));
		assert(node1.inChildNodeRecursive(&node4));
		assert(node1.inChildNodeRecursive(&node5));
		assert(!node1.inChildNodeRecursive(&node6));
	}

};

#endif /*TEST_ANIMATION_H_*/
